package com.moody.hibernate.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.EnterBalanceSheet;
import com.moody.hibernate.domain.ReportModel;

/**
 * A data access object (DAO) providing persistence and search support for
 * ReportModel entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.ReportModel
 * @author MyEclipse Persistence Tools
 */

public class ReportModelDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(ReportModelDAO.class);
	// property constants
	public static final String DISPLAY_NAME = "displayName";
	public static final String FINANCE_ITEM_NO = "financeItemNo";
	public static final String ITEM_DEF = "itemDef";

	protected void initDao() {
		// do nothing
	}

	public void save(ReportModel transientInstance) {
		log.debug("saving ReportModel instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ReportModel persistentInstance) {
		log.debug("deleting ReportModel instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ReportModel findById(com.moody.hibernate.domain.ReportModelId id) {
		log.debug("getting ReportModel instance with id: " + id);
		try {
			ReportModel instance = (ReportModel) getHibernateTemplate().get(
					"com.moody.hibernate.ReportModel", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ReportModel instance) {
		log.debug("finding ReportModel instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ReportModel instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ReportModel as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByDisplayName(Object displayName) {
		return findByProperty(DISPLAY_NAME, displayName);
	}

	public List findByFinanceItemNo(Object financeItemNo) {
		return findByProperty(FINANCE_ITEM_NO, financeItemNo);
	}

	public List findByItemDef(Object itemDef) {
		return findByProperty(ITEM_DEF, itemDef);
	}

	public List findAll() {
		log.debug("finding all ReportModel instances");
		try {
			String queryString = "from ReportModel";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ReportModel merge(ReportModel detachedInstance) {
		log.debug("merging ReportModel instance");
		try {
			ReportModel result = (ReportModel) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ReportModel instance) {
		log.debug("attaching dirty ReportModel instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ReportModel instance) {
		log.debug("attaching clean ReportModel instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ReportModelDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ReportModelDAO) ctx.getBean("ReportModelDAO");
	}
	
	/*
	 * 以下方法由自己添加
	 */
	
	//批量插入
	public void insertReport(List<ReportModel> list){
		try{
			//如果已有数据则不插入更新
			if(list!=null && list.size()>0){
				List<ReportModel> list1 = this.findByReportNo(list.get(0).getId().getReportNo());
				if(list1!=null && list1.size()>0){
					//有数据 不执行操作
				}else{
					//如果无数据则进行更新
					getHibernateTemplate().saveOrUpdateAll(list);
				}
			}else{
				log.info("插入数据为空");
			}
			
		}catch (Exception e){
			log.error("insert error", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<ReportModel> findByReportNo(String reportno) {
		List<ReportModel> list = new ArrayList<ReportModel>();
		try{
			String hql = "from ReportModel as rm where rm.id.reportNo = ?";
			list = getHibernateTemplate().find(hql,reportno);
		}catch (Exception e){
				log.error("query error", e);
		}
		return list;
	}
}